package com.iflytek.esb.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iflytek.esb.dao.MessageDao;
import com.iflytek.esb.domain.Id;
import com.iflytek.esb.domain.PersistenceMessage;

@Service
public class MessageService {
	
	@Autowired
	private MessageDao messageDao;

	public PersistenceMessage getMessageById(long id) {
		return messageDao.getMessageById(id);
	}
	
	public List<PersistenceMessage> getMessageByEntry(long entryId) {
		return messageDao.getMessageByEntry(entryId);
	}
	
	public PersistenceMessage createMessage(long entryId, Map<String, String> headers, String content) {
		Id id = new Id();
		messageDao.newMessageId(id);
		
		PersistenceMessage pm = new PersistenceMessage();
		pm.setContent(content);
		pm.setEntryId(entryId);
		pm.setHeaders(headers);
		pm.setId(id.getMaxId());
		
		messageDao.insertMessage(pm);
		return pm;
	}
	
	public PersistenceMessage createMessage(Map<String, String> headers, String content) {
		return createMessage(0, headers, content);
	}
	
	public void updateMessage(PersistenceMessage persistenceMessage) {
		messageDao.updateMessage(persistenceMessage);
	}
	
	public void deleteMessage(long id) {
		messageDao.deleteMessage(id);
	}
}
